<template>
  <el-container class="Home-container">
    <!--头部-->
    <el-header class="Home-header">
      <div>
        <img src="~assets/img/jerry.png" alt="">
        <p>电商后台管理系统</p>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--页面主体区域-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse? '64px':'200px'" class="Home-aside">
        <div class="toggle-menu" @click="changeCollapse">{{isCollapse? '展开':'折叠'}}</div>
                  <!--element-ui导航菜单的属性太好用了  特别是router属性 666-->
        <el-menu background-color="#303238" text-color="#fff" 
                 active-text-color="#409eff" unique-opened  router
                 :collapse="isCollapse" :collapse-transition="false" 
                 :default-active="$route.path">
            <!--一级菜单-->     <!--将数字item.id转化成字符串-->
            <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
              <template slot="title">
                <i :class="icons[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!--二级菜单-->
              <el-menu-item :index="'/' + subItem.path" 
                            v-for="subItem in item.children" :key="subItem.id">
                <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧主体内容-->
      <el-main class="Home-main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default{
    data() {
      return {
        menulist: [],
        icons: {
          125: "iconfont icon-user",
          103: "iconfont icon-tijikongjian",
          101: "iconfont icon-shangpin",
          102: "iconfont icon-danju",
          145: "iconfont icon-baobiao"
        },
        isCollapse: false,
      }
    },
    methods:{
      logout() {
        window.sessionStorage.clear()  //清空token
        this.$router.push('/login'); //跳转到登录页
      },
      changeCollapse() {
        this.isCollapse = !this.isCollapse;
      }
    },
    async created() {
      const res = await this.$http.get('menus');
      // console.log(res);
      if(res.data.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data.data;
    }
  }
</script>

<style scoped>
.Home-container{
  height: 100%;
}

.Home-header{
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0px;
}

.Home-header img{
  width: 60px;
  height: 60px;
  border-radius: 50%;
}

.Home-header div{
  display: flex;
  align-items: center;
}

.Home-header div p{
  margin-left: 15px;
  color: #fff;
  font-size: 20px;
}

.Home-aside{
  background-color: #303238;
  transition:width 0.5s;   
}

.Home-aside .el-menu{
    border-right: none;
}

.toggle-menu{
  height: 30px;
  color: #ccc;
  text-align: center;
  background-color: #4a5064;
  font-size: 10;
  line-height: 30px;
  cursor: pointer;
}

.iconfont{
  margin-right: 10px;
}

.Home-main{
  background-color: #eaedf1;
}



</style>